package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Orders;
import com.itheima.bean.PageParams;
import com.itheima.bean.User;
import com.itheima.common.R;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author hebing
 * @version 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParams params) {
        try {
            //调用service层进行分页查询
            IPage<Orders> ordersIPage = orderService.findPage(params);
            return R.success(ordersIPage);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败");
        }
    }

    /**
     * 修改订单状态
     *
     * @return
     */
    @PutMapping
    public R updateStatus(@RequestBody Orders orders) {
        //调用service层进行修改
        int rows = orderService.updateStatus(orders);
        //判定
        if (rows > 0) {
            return R.success("修改状态成功");
        } else {
            return R.error("修改状态失败");
        }
    }

    /**
     * 提交订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders, HttpSession session){
        User user = (User) session.getAttribute("user");
        //调用service进行添加记录
        Orders order=orderService.submit(orders,user);
        //判断
        if(order!=null){
            return R.success(order);
        }else {
            return R.error("支付失败");
        }
    }
}
